package com.itHsuSE.object;

public class EqualsExer {
    public static void main(String[] args) {
        Person p1 = new Person("jack", 1, '男');
        Person p2 = new Person("jack", 1, '男');
        System.out.println(p1.equals(p2));
        System.out.println(61==61.0);
    }
}

class Person {
    private String name;
    private int age;
    private char gender;

    @Override
    public boolean equals(Object obj) {
        //如果比较的两个对象是同个对象，则直接返回true
        if (this == obj) {
            return true;
        }
        //类型判断
        if (obj instanceof Person) {//是Person才比较
            //进行向下转型 => 因为要取得传进来的对象 obj 的属性，不向下转型取不到
            Person p = (Person) obj;
            return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        result = 31 * result + (int) gender;
        return result;
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}
